package reone.android.mandown.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import reone.android.mandown.Activity.sup.BaseActivity;
import reone.android.mandown.R;
import reone.android.mandown.db.ScoreDBManager;
import reone.android.mandown.util.AppTools;
import reone.android.mandown.util.ConstantInfo;

public class PrefsActivity extends BaseActivity implements OnClickListener,
		SeekBar.OnSeekBarChangeListener {

	private SharedPreferences mBaseSettings;

	private SharedPreferences mRankingSettings;

	private EditText mUserNameEditText;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.options);

		mBaseSettings = PreferenceManager.getDefaultSharedPreferences(this);

		CheckBox vibrateCheckbox = (CheckBox) findViewById(R.id.options_vibrate_checkbox);
		vibrateCheckbox.setChecked(mBaseSettings.getBoolean(
				ConstantInfo.PREFERENCE_KEY_VIBRATE, true));
		vibrateCheckbox
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							mBaseSettings.edit().putBoolean(
									ConstantInfo.PREFERENCE_KEY_VIBRATE, true)
									.commit();
						} else {
							mBaseSettings.edit().putBoolean(
									ConstantInfo.PREFERENCE_KEY_VIBRATE, false)
									.commit();
						}
					}
				});

		CheckBox soundsCheckbox = (CheckBox) findViewById(R.id.options_sounds_checkbox);
		soundsCheckbox.setChecked(mBaseSettings.getBoolean(
				ConstantInfo.PREFERENCE_KEY_SOUNDS, true));
		soundsCheckbox
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							mBaseSettings.edit().putBoolean(
									ConstantInfo.PREFERENCE_KEY_SOUNDS, true)
									.commit();
						} else {
							mBaseSettings.edit().putBoolean(
                                    ConstantInfo.PREFERENCE_KEY_SOUNDS, false)
									.commit();
						}
					}
				});

		CheckBox showTipsCheckbox = (CheckBox) findViewById(R.id.options_showtips_checkbox);
		showTipsCheckbox.setChecked(mBaseSettings.getBoolean(
				ConstantInfo.PREFERENCE_KEY_SHOWTIPS, true));
		showTipsCheckbox
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        if (isChecked) {
                            mBaseSettings.edit().putBoolean(
                                    ConstantInfo.PREFERENCE_KEY_SHOWTIPS, true)
                                    .commit();
                        } else {
                            mBaseSettings
                                    .edit()
                                    .putBoolean(
                                            ConstantInfo.PREFERENCE_KEY_SHOWTIPS,
                                            false).commit();
                        }
                    }
                });

		SeekBar seekBar = (SeekBar) findViewById(R.id.velocityController);
		seekBar.setProgress(mBaseSettings.getInt(
                ConstantInfo.PREFERENCE_KEY_POWER, 40));
		seekBar.setOnSeekBarChangeListener(this);

		mRankingSettings = AppTools.getSharedPreferences();

		mUserNameEditText = (EditText) findViewById(R.id.options_username_edittext);
		mUserNameEditText.setText(mRankingSettings.getString(
				ConstantInfo.PREFERENCE_KEY_RANKING_NAME, ""));

		TextView bestRecordTextView = (TextView) findViewById(R.id.options_best_record_textview);
		bestRecordTextView.setText(""
				+ bestRecordTextView.getText()
				+ mRankingSettings.getInt(
						ConstantInfo.PREFERENCE_KEY_RANKING_SCORE, 0));

		Button okayButton = (Button) findViewById(R.id.options_okay_button);
		okayButton.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.options_okay_button:
            String userName = null;
            if (mUserNameEditText.getText() != null) {
                userName = mUserNameEditText.getText().toString()
                        .replace("\n", " ").trim();
            }
            if (userName == null || "".equals(userName)) {
                showToast(R.string.options_toast_username_null);
                return;
            } else if (userName.length() > 20) {
                showToast(R.string.options_toast_username_too_long);
                return;
            }
            mRankingSettings.edit().putString(
                    ConstantInfo.PREFERENCE_KEY_RANKING_NAME, userName).commit();
			finish();
			break;
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		mBaseSettings.edit().putInt(ConstantInfo.PREFERENCE_KEY_POWER,
				seekBar.getProgress()).commit();
	}

	private void showToast(int strId) {
		Toast toast = Toast.makeText(this, strId, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP, 0, 220);
		toast.show();
	}
}
